<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules" v-bind="layout">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-model-item label="选择员工" prop="employeeId">
              <j-select-employee-modal
                v-model="model.employeeId"
                :multiple="false"
                @change="handleEmployeeChange"
                placeholder="请选择员工"
                :disabled="!!model.id"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="员工姓名">
              <a-input v-model="model.employeeName" disabled/>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-model-item label="工种类型" prop="workTypeId">
              <a-select
                v-model="model.workTypeId"
                placeholder="请选择工种"
                @change="handleWorkTypeChange"
                :disabled="!!model.id">
                <a-select-option
                  v-for="workType in workTypeOptions"
                  :key="workType.id"
                  :value="workType.id">
                  {{ workType.workTypeName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="技能等级" prop="skillLevel">
              <j-dict-select-tag v-model="model.skillLevel" dictCode="skill_level" placeholder="请选择技能等级"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-model-item label="认证日期" prop="certificationDate">
              <a-date-picker
                v-model="model.certificationDate"
                placeholder="请选择认证日期"
                format="YYYY-MM-DD"
                style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="到期日期" prop="expiryDate">
              <a-date-picker
                v-model="model.expiryDate"
                placeholder="请选择到期日期"
                format="YYYY-MM-DD"
                style="width: 100%"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-model-item label="认证人" prop="certifier">
              <a-input v-model="model.certifier" placeholder="请输入认证人"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" prop="status">
              <j-dict-select-tag v-model="model.status" dictCode="valid_status" placeholder="请选择状态"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="备注" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注" :rows="2"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import moment from 'moment'

  export default {
    name: 'EmployeeWorktypeModal',
    components: {
    },
    data () {
      return {
        layout: {
          labelCol: {
            xs: { span: 24 },
            sm: { span: 5 },
          },
          wrapperCol: {
            xs: { span: 24 },
            sm: { span: 16 },
          },
        },
        title:"",
        visible: false,
        model: {},
        confirmLoading: false,
        workTypeOptions: [],
        form: this.$form.createForm(this),
        validatorRules: {
          employeeId: [
            { required: true, message: '请选择员工!' }
          ],
          workTypeId: [
            { required: true, message: '请选择工种!' }
          ],
          skillLevel: [
            { required: true, message: '请选择技能等级!' }
          ],
          status: [
            { required: true, message: '请选择状态!' }
          ]
        },
        url: {
          add: "/mes/employee-worktype/add",
          edit: "/mes/employee-worktype/edit",
          getAllWorkTypes: "/mes/worktype/all"
        }
      }
    },
    created () {
      this.loadWorkTypes();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        
        // 处理日期格式
        if (this.model.certificationDate) {
          this.model.certificationDate = moment(this.model.certificationDate);
        }
        if (this.model.expiryDate) {
          this.model.expiryDate = moment(this.model.expiryDate);
        }
        
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(this.model)
        })
        if(!this.model.id){
          this.title = "新增员工工种分配";
          // 设置默认值
          this.model.skillLevel = 1;
          this.model.status = 1;
          this.model.certificationDate = moment();
        }else{
          this.title = "编辑员工工种分配";
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            
            // 处理日期格式
            let submitModel = Object.assign({}, this.model);
            if (submitModel.certificationDate) {
              submitModel.certificationDate = submitModel.certificationDate.format('YYYY-MM-DD');
            }
            if (submitModel.expiryDate) {
              submitModel.expiryDate = submitModel.expiryDate.format('YYYY-MM-DD');
            }
            
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl, submitModel, method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      handleCancel () {
        this.close();
      },
      handleEmployeeChange(employeeId, employeeInfo) {
        this.model.employeeName = employeeInfo ? employeeInfo.empName : '';
      },
      handleWorkTypeChange(workTypeId) {
        const workType = this.workTypeOptions.find(item => item.id === workTypeId);
        if (workType) {
          this.model.workTypeCode = workType.workTypeCode;
          this.model.workTypeName = workType.workTypeName;
        }
      },
      loadWorkTypes() {
        getAction(this.url.getAllWorkTypes).then((res) => {
          if (res.success) {
            this.workTypeOptions = res.result || [];
          }
        });
      }
    }
  }
</script>
